package cn.base.学生管理;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManager {
    Scanner sc = new Scanner(System.in);
    //创建学生集合
    ArrayList<Student> list01 = new ArrayList<>();

    public void start() {
        ArrayList<Student> list01 = new ArrayList<>();
        boolean flag = true;
        Scanner sc = new Scanner(System.in);
        Student s = new Student();
        while (flag) {
            System.out.println("====================");
            System.out.println("======学生管理系统===");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查看学生");
            System.out.println("5.退出");
            System.out.print("请输入您的选择：");
            int selection = sc.nextInt();
            switch (selection) {
                case 1: {
                    addStudent();
                    break;
                }
                case 2: {
                    deleteStudent();
                    break;
                }
                case 3: {
                    updateStudent();
                    break;
                }
                case 4: {
                    selectStudent();
                    break;
                }
                case 5: {
                    System.out.println("再见");
                    flag = false;
                    break;
                }
                default: {
                    System.out.println("输入有误请重新输入");
                }

            }
        }
    }

    /**
     * 添加学生
     */
    private void addStudent() {
        String code;
        while (true) {
            System.out.print("请输入学号：");
            code = sc.next();
            int index = getIndexById(code);
            if (index >= 0) {
                System.out.println("学号已经存在！！");
            } else {
                break;
            }
        }
        System.out.print("请输入姓名：");
        String name = sc.next();
        System.out.print("请输入年龄：");
        int age = sc.nextInt();
        System.out.print("请输入生日：");
        String birthday = sc.next();
        list01.add(new Student(code, name, age, birthday));
        System.out.println("添加成功");
    }

    /**
     * 删除学生
     */
    private void deleteStudent() {
        System.out.print("请输入要删除的学生学号：");
        String code = sc.next();
        int index = getIndexById(code);
        if (index == -1) {
            System.out.println("学生不存在");
        } else {
            list01.remove(index);
            System.out.println("删除成功！！！");
        }
    }

    /**
     * 判断学生是否存在，不存在返回-1，存在返回当前学生的索引
     *
     * @param code
     * @return
     */
    public int getIndexById(String code) {
        for (int i = 0; i < list01.size(); i++) {
            Student s = list01.get(i);
            if (code.equals(s.getCode())) {
                return i;
            }
        }
        return -1;
    }

    /**
     * 修改学生
     */
    private void updateStudent() {
        System.out.print("请输入需要修改学生的学号：");
        String code = sc.next();
        int index = getIndexById(code);
        if (index == -1) {
            System.out.println("学生不存在");
        } else {
            System.out.print("请输入新的姓名：");
            list01.get(index).setName(sc.next());
            System.out.print("请输入新的年龄：");
            list01.get(index).setAge(sc.nextInt());
            System.out.print("请输入新的生日：");
            list01.get(index).setBirthday(sc.next());
            System.out.println("修改成功！！！");
        }
    }

    /**
     * 查看学生
     */
    private void selectStudent() {
        System.out.println("学号\t姓名\t年龄\t生日");
        for (int i = 0; i < list01.size(); i++) {
            System.out.print(list01.get(i).getCode() + "\t" + list01.get(i).getName()
                    + "\t" + list01.get(i).getAge() + "\t" + list01.get(i).getBirthday());
        }
        System.out.println();
    }


}
